package com.github.cdisk.xtrans.ui.toolbox;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.github.cdisk.xtrans.annotiation.Component;
import com.github.cdisk.xtrans.core.BaseComponent;
import com.github.cdisk.xtrans.ui.ComponentUI;
import com.github.cdisk.xtrans.ui.Context;
import com.github.cdisk.xtrans.ui.Operation;
import com.github.cdisk.xtrans.util.PackageScanUtil;

/**
 * 左侧工具栏加载类
 * 自动读取有的组件，并加载到工具栏中
 * @author Cdisk
 *
 */
public class ComponentBoxLoader {

	public static List<Class<BaseComponent>> COMPONENTS = PackageScanUtil.scan("com.github.cdisk.xtrans.component", BaseComponent.class);

	public static void initComponents(Context context) {
		List<String> items = new ArrayList<String>();
		for (Class<BaseComponent> component : COMPONENTS) {
			Component compa = component.getAnnotation(Component.class);
			if (compa == null) continue;

			String group = compa.group();
			if (items.indexOf(group) == -1) {
				items.add(group);
			}
		}

		for (String i : items) {
			ExpandItem expandItem = new ExpandItem(context.getToolbox(), SWT.NONE);
			expandItem.setExpanded(true);
			expandItem.setText(i);

			Composite composite = new Composite(context.getToolbox(), SWT.NONE);
			expandItem.setControl(composite);
			expandItem.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
			RowLayout layout = new RowLayout(SWT.VERTICAL);
			layout.wrap = false;
			composite.setLayout(layout);
			composite.addControlListener(new ControlAdapter() {
				@Override
				public void controlResized(ControlEvent e) {
					Control[] buttons = composite.getChildren();
					for (Control button : buttons) {
						button.setLayoutData(new RowData(composite.getSize().x - 6, SWT.DEFAULT));
					}
				}
			});
			
			for (Class<BaseComponent> component : COMPONENTS) {
				Component compa = component.getAnnotation(Component.class);
				if (compa == null) continue;

				String group = compa.group();
				if (group.equals(i)) {
					Button button = new Button(composite, SWT.TOGGLE);
//					System.out.println(ComponentSet.class.getResource("/").getPath() + "icon/" + compa.icon());
					Image image = SWTResourceManager.getImage(ComponentBoxLoader.class.getResource("/").getPath() + "icon/" + compa.icon());
					button.setImage(image);
					button.setText(compa.value());
					button.setData(component);
					button.addSelectionListener(new SelectionListener() {
						
						@Override
						public void widgetSelected(SelectionEvent e) {
							if (button.getSelection()) {
								/* 清空其他选中项 */
								ExpandItem[] expandItems = context.getToolbox().getItems();
								for (ExpandItem item : expandItems) {
									Control[] controls = ((Composite) item.getControl()).getChildren();
									for (Control control : controls) {
										((Button) control).setSelection(false);
									}
								}
								/* 设置新的选中项 */
								try {
									BaseComponent comp = component.newInstance();
									
									/* 起一个不重复的新名字 */
									String newId = "新的" + comp.getClass().getSimpleName() + " - ";
									Integer maxId = 0;
									for (BaseComponent c : Operation.CONFIG.components) {
										String cId = c.getId();
										if (cId.startsWith(newId)) {
											Integer cIdNum = NumberUtils.createInteger(cId.replaceAll(newId, ""));
											if (cIdNum > maxId) maxId = cIdNum;
										}
									}
									newId += String.valueOf(maxId + 1);
									
									/* 设置名字，并准备加载。 */
									comp.setId(newId);
									comp.setUi(new ComponentUI());
									context.getCanvas().setReadyComponent(comp);
								} catch (InstantiationException e1) {
									e1.printStackTrace();
								} catch (IllegalAccessException e1) {
									e1.printStackTrace();
								}
							} else {
								context.getCanvas().setReadyComponent(null);
							}
						}
						
						@Override
						public void widgetDefaultSelected(SelectionEvent e) {
						}
					});
				}
			}
		}
	}
}
